package com.practice.book.config;

import com.practice.book.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.resource.NoResourceFoundException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-01-25
 * Time: 15:13
 */
@Slf4j
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {
    @ExceptionHandler
    public Object handler(Exception e) {
        log.error("发生异常,e",e);
        return Result.loginButFail();
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler
    public Object handler(ArrayIndexOutOfBoundsException e) {
        log.error("发生异常,e",e);
        return Result.loginButFail("发生数组越界异常...");
    }


    @ExceptionHandler
    public Object handler(NullPointerException e) {
        log.error("发生异常,e",e);
        return Result.loginButFail("发生NullPointException异常...");
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler
    public Object handler(NoResourceFoundException e) {
        log.error("发生异常,e:{}",e.getDetailMessageCode(),e.getResourcePath());
        return Result.loginButFail("发生NoResourceFoundException异常...");
    }
}
